#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<string>

using namespace std;

//类型强转
class A
{
public:
	A(int a = 0)
		:_a(a)
	{

	}

	~A()
	{
		_a = 0;
	}

	explicit operator int()
	{
		return _a;
	}

	operator bool()
	{
		return _a;
	}
private:
	int _a;

};

void test1()
{
	//单参数的隐式类型转换
	A aa1 = 1;

	int i = aa1;
	double d = aa1;

	cout << i << " " << d << endl;

}

//cin的返回值
void test2()
{
	string str;
	while (cin >> str)  //string:: istream& operator>>(istream& cin,string& str).operator bool()   ctrl+Z退出
	{
		cout << str << endl;
	}
}

void test3()
{
	int a, b;
	while (cin >> a >> b)  //cin.operator>>(a)>>operator(b).operator bool()
	{
		cout << a << " " << b << endl;
	}
}

class Date
{
	friend ostream& operator << (ostream& out, const Date& d);
	friend istream& operator >> (istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{}
	operator bool()
	{
		// 这里是随意写的，假设输入_year为0，则结束
		if (_year == 0)
			return false;
		else
			return true;
	}

	operator string()
	{
		string str;
		str += to_string(_year);
		str += ' ';
		str += to_string(_month);
		str += ' ';
		str += to_string(_day);
		return str;
	}

private:
	int _year;
	int _month;
	int _day;
};
istream& operator >> (istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
ostream& operator << (ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day;
	return out;
}

//C语言中的文件读写
void test4()
{
	Date d(2024, 8, 11);
	FILE* fin = fopen("file.txt", "w");
	//二进制写入
	//fwrite(&d, sizeof(d), 1, fin);
	//fclose(fin);

	//写入Date类
	string str = d;
	fputs(str.c_str(), fin);
	fclose(fin);
}

//C++中的文件读写
#include<fstream>
void test5()
{
	Date d(2024, 8, 11);

	//ofstream ofs("file1.txt", ios_base::out | ios_base::binary);
	ofstream ofs("file1.txt");
	// 二进制的方式
	//ofs.write((const char*)&d, sizeof(d));
	// 文本的方式
	ofs << d;

}

struct ServerInfo
{
	char _address[32];
	int _port;
	Date _date;
	// 二进制读写不能用string vector这样的对象存数据
	// 否则写出去就是一个指针，进程结束就是野指针，另一个进程再读进来进坑了
	//string _address;
};
struct ConfigManager
{
public:
	ConfigManager(const char* filename)
		:_filename(filename)
	{}

	void WriteBin(const ServerInfo& info)
	{
		ofstream ofs(_filename, ios_base::out | ios_base::binary);
		ofs.write((const char*)&info, sizeof(info));
		//ofs.close();

	}

	void ReadBin(ServerInfo& info)
	{
		ifstream ifs(_filename, ios_base::in | ios_base::binary);
		ifs.read((char*)&info, sizeof(info));
		//ifs.close();

	}

	// C++文件流的优势就是可以对内置类型和自定义类型，都使用一样的方式，去流插入和流提取数据
	// 这里自定义类型Date需要重载>> 和 <<
	// istream& operator >> (istream& in, Date& d)
	// ostream& operator << (ostream& out, const Date& d)

	void WriteText(const ServerInfo & info)
	{
		ofstream ofs(_filename);
		ofs << info._address << " " << info._port << " " << info._date;
		//ofs.close();
	}

	void ReadText(ServerInfo& info)
	{
		ifstream ifs(_filename);
		ifs >> info._address >> info._port >> info._date;
		//ifs.close();

	}
private:
	string _filename; // 配置文件
};

void test6()
{
	//二进制写文件
	ServerInfo winfo = { "192.0.0.1111111111111111111", 80, { 2022, 4, 10 } };
	ConfigManager cm("test.bin");
	cm.WriteBin(winfo);
	

	//二进制读文件
	ConfigManager cf_bin("test.bin");
	ServerInfo rbinfo;
	cf_bin.ReadBin(rbinfo);
	cout << rbinfo._address << "  " << rbinfo._port << "  " << rbinfo._date << endl;

}

void test7()
{
	//文本写文件
	ServerInfo winfo = { "192.0.0.1111111111111111111", 80, { 2022, 4, 10 } };
	ConfigManager cm("test.txt");
	cm.WriteText(winfo);

	//文本读文件
	ConfigManager cf_text("test.txt");
	ServerInfo rtinfo;
	cf_text.ReadText(rtinfo);
	cout << rtinfo._address << "  " << rtinfo._port << "  " << rtinfo._date << endl;

}
#include"sstream"
void test8()
{
	Date d(2024, 8, 11);
	ostringstream oss;
	oss << d;
	string str = oss.str();
	cout << d << endl;

	string str1 = "2024 8 11";
	istringstream iss(str1);
	Date d1;
	iss >> d1;
	cout << d1 << endl;
}

void test9()
{
	string _filename = "study.txt";

	ofstream ofs(_filename);
	Date d(2024, 8, 11);
	ofs << d;
	cout << d << endl;
	ofs.close();

	ifstream ifs(_filename);
	Date d1;
	ifs >> d1;
	cout << d1 << endl;
	ifs.close();

}

struct ChatInfo
{
	string _name; // 名字
	int _id;      // id
	Date _date;   // 时间
	string _msg;  // 聊天信息
};

void test10()
{
	ChatInfo winfo = { "张三",123456,{2024,8 ,11},"晚上一起看电影" };

	stringstream oss;
	oss << winfo._name << " " << winfo._id << " " << winfo._date<< " " << winfo._msg;
	string str = oss.str();
	cout << str << endl;

	ChatInfo rinfo;
	stringstream iss(str);
	iss >> rinfo._name >> rinfo._id >> rinfo._date >> rinfo._msg;
	cout << "-------------------------------------------------------" << endl;
	cout << "姓名：" << rinfo._name << "(" << rinfo._id << ") ";
	cout << rinfo._date << endl;
	cout << rinfo._name << ":>" << rinfo._msg << endl;
	cout << "-------------------------------------------------------" << endl;
}

int main()
{
	test1();

	//test2();

	//test3();

	test4();

	test5();

    test6();
	

	test7();

	test8();

	test9();

	//ifstream ifs("Test.cpp");
	//char ch;
	//while (ifs.get(ch))
	//{
	//	cout << ch;
	//}

	test10();



	return 0;
}